<div class="modal-header">
    <button type="button" class="close _close" data-status="{{ $status }}" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">发货单备货</h4>
</div>
<form id="prepare-form" action="/xqc/delivery/fba-ajax" method="post" onsubmit="return checkForm();">
    <div class="modal-body">
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>SKU</th>
                    <th width="25%">产品名称</th>
                    <th>亚马逊SKU</th>
                    <th>ASIN</th>
                    <th>FNSKU</th>
                    <th>申请发货数量</th>
                    <th>本次备货数量</th>
                    <th>已备货数量</th>
                </tr>
                </thead>
                <tbody>
                @foreach($delivery->detail as $k => $item)
                    <tr>
                        <td>{{ $item->sku->psku_code ?? '' }}</td>
                        <td>{{ $item->sku->psku_cn ?? '' }}</td>
                        <td>{{ $item->seller_sku }}</td>
                        <td>{{ $item->asin }}</td>
                        <td>{{ $item->fnsku }}</td>
                        <td>{{ $item->apply_num }}</td>
                        <td>
                            <input type="hidden" name="param[detail][{{ $k }}][id]" value="{{ $item->id }}">
                            <input type="text" class="form-control ship_num_" style="margin: auto; width: 130px; text-align: right;" name="param[detail][{{ $k }}][ship_num]" data-max="{{ $item->apply_num - $item->ship_num }}" value="{{ $item->apply_num - $item->ship_num }}">
                        </td>
                        <td><span class="label label-{{ $item->apply_num > $item->ship_num ? 'danger' : 'info'}}">{{ $item->ship_num }}</span></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <div class="modal-footer">
        {{ csrf_field() }}
        <input type="hidden" name="action" value="doPrepare">
        <input type="hidden" name="param[id]" value="{{ $id }}">
        <button type="button" class="btn btn-default _close" data-status="{{ $status }}" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">提交</button>
    </div>
</form>
<script language="JavaScript" type="text/javascript">
    function checkForm(){
        var flag = true;
        $(".ship_num_").each(function(i,item){
            if(item.value == '') flag = false;
        });
        return flag;
    }

    $(function () {
        $("#prepare-form").ajaxForm(function (data) {
            $("#listCommonModal").modal('hide');
            $(".modal-backdrop").remove();
            $('body').removeClass('modal-open');
            $.pjax.reload('#pjax-container');
            if (typeof data === 'object') {
                if (data.status) {
                    toastr.success('提交成功 !');
                } else {
                    swal(data.message, '', 'error');
                }
            }
        });
        $(".ship_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    if($(this).val() > $(this).data('max')){
                        $(this).val($(this).data('max'));
                    }
                }
            }
        );
        $('._close').on('click', function(e) {
            e.preventDefault();
            var status = $(this).data('status');
            if(status == 2){
                $.pjax.reload('#pjax-container');
            }
        });
    });
</script>
